<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="name">
      <el-input v-model="dataForm.name" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="navbarLevel">
      <el-input v-model="dataForm.navbarLevel" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="summary">
      <el-input v-model="dataForm.summary" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="parentUid">
      <el-input v-model="dataForm.parentUid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="url">
      <el-input v-model="dataForm.url" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="icon">
      <el-input v-model="dataForm.icon" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="isShow">
      <el-input v-model="dataForm.isShow" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="isJumpExternalUrl">
      <el-input v-model="dataForm.isJumpExternalUrl" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="sort">
      <el-input v-model="dataForm.sort" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="status">
      <el-input v-model="dataForm.status" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="是否删除:0否 1是" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="是否删除:0否 1是"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          name: '',
          navbarLevel: '',
          summary: '',
          parentUid: '',
          url: '',
          icon: '',
          isShow: '',
          isJumpExternalUrl: '',
          sort: '',
          status: '',
          createTime: '',
          updateTime: '',
          isDelete: ''
        },
        dataRule: {
          name: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          navbarLevel: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          summary: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          parentUid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          isShow: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          isJumpExternalUrl: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除:0否 1是不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/user/webnavbar/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.webNavbar.name
                this.dataForm.navbarLevel = data.webNavbar.navbarLevel
                this.dataForm.summary = data.webNavbar.summary
                this.dataForm.parentUid = data.webNavbar.parentUid
                this.dataForm.url = data.webNavbar.url
                this.dataForm.icon = data.webNavbar.icon
                this.dataForm.isShow = data.webNavbar.isShow
                this.dataForm.isJumpExternalUrl = data.webNavbar.isJumpExternalUrl
                this.dataForm.sort = data.webNavbar.sort
                this.dataForm.status = data.webNavbar.status
                this.dataForm.createTime = data.webNavbar.createTime
                this.dataForm.updateTime = data.webNavbar.updateTime
                this.dataForm.isDelete = data.webNavbar.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/webnavbar/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'name': this.dataForm.name,
                'navbarLevel': this.dataForm.navbarLevel,
                'summary': this.dataForm.summary,
                'parentUid': this.dataForm.parentUid,
                'url': this.dataForm.url,
                'icon': this.dataForm.icon,
                'isShow': this.dataForm.isShow,
                'isJumpExternalUrl': this.dataForm.isJumpExternalUrl,
                'sort': this.dataForm.sort,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
